﻿#region Usings

using System.Configuration;
using System.Linq;

#endregion

namespace XPressify.Data
{
    public class XSettings : ConfigurationSection
    {
        private static XSettings _settings;

        private XSettings()
        {
        }

        public static XSettings Instance
        {
            get
            {
                if (_settings == null)
                {
                    _settings = ConfigurationManager.GetSection("xSettings") as XSettings;
                }
                return _settings;
            }
        }

        [
            ConfigurationProperty("general", IsDefaultCollection = false),
            ConfigurationCollection(typeof (SettingsCollection), AddItemName = "add", ClearItemsName = "clearAll", RemoveItemName = "remove")
        ]
        public SettingsCollection GeneralSettings
        {
            get { return this["general"] as SettingsCollection; }
        }

        public string SiteName
        {
            get
            {
                if (GeneralSettings != null && GeneralSettings["SiteName"] != null)
                {
                    return GeneralSettings["SiteName"].Value;
                }
                return null;
            }
        }

        public string SiteUrl
        {
            get
            {
                if (GeneralSettings != null && GeneralSettings["SiteUrl"] != null)
                {
                    return GeneralSettings["SiteUrl"].Value;
                }
                return null;
            }
        }
    }

    public class Setting : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string) this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("value", IsRequired = true)]
        public string Value
        {
            get { return (string) this["value"]; }
            set { this["value"] = value; }
        }
    }

    public class SettingsCollection : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
        }

        public Setting this[int index]
        {
            get { return (Setting) BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        public new Setting this[string key]
        {
            get { return (Setting)BaseGet(key); }
        }

        public void Add(Setting element)
        {
            BaseAdd(element);
        }

        public void Clear()
        {
            BaseClear();
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new Setting();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((Setting) element).Name;
        }


        public void Remove(Setting element)
        {
            BaseRemove(element.Name);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }
    }
}